import request from "@/utils/request";
import type { ApifoxModal } from "./tieba";
import type { typeUserInfo } from "./user";
// 贴吧 某个 类型
export interface typeBarItem {
  _id: string;
  barName: string;
  description: string;
  barLogo: string;
  creator: string;
  beSubCount: number;
  isSub?: boolean;
}
export type typeBarList = typeBarItem[];
export interface typeBarReturn {
  count: number;
  result: typeBarList;
}

// 查询贴吧 返回值类型
export interface Like {
  _id: string;
  barName: string;
  description: string;
  barLogo: string;
  creator: string;
  beSubCount: number;
  propositionCount: number;
  isSub: boolean;
}

export interface searchBarData {
  like: Like[];
  count: number;
}

// 贴子 类型
interface imageList {
  filename: string;
  url: string;
}
export interface typeDynamicItem {
  _id: string;
  title: string;
  description: string;
  user: typeUserInfo;
  bar: string;
  commentCount: number;
  beCollectCount: number;
  imageList: imageList[];
}
export type typeDynamicList = typeDynamicItem[];
// 请求得到的数据
export interface typeDynamic {
  allTip: typeDynamicList;
  count: number;
}

// 创建帖子参数类型
export interface typeCreateDynamicParam {
  bar: string;
  description: string;
  imageList: ImageList[];
  title: string;
  user: string;
}

export interface ImageList {
  filename: string;
  url: string;
}

export default {
  //未登录获取 帖吧的分页列表
  requestAllBar: (page: number = 1, pagesize: number = 12) => {
    return request.get<any, typeBarReturn>(
      `bar?pagesize=${pagesize}&page=${page}`
    );
  },

  // 登录后获取 帖子的分页列表
  requestAllBarByToken: (page: number = 1, pagesize: number = 12) => {
    return request.get<any, typeBarReturn>(
      `bar/barByToken?pagesize=${pagesize}&page=${page}`
    );
  },

  // 获取 帖子的分页列表
  requestDynamic: (
    barId: string,
    page: number,
    pagesize: number,
    name?: string
  ) => {
    return request.get<any, typeDynamic>(
      `proposition/getProposition/${barId}?page=${page}&pagesize=${pagesize}&name=${name}`
    );
  },
  // 贴吧 模糊查询
  requestSearchBar: (name: string, page: number = 1, pagesize: number = 5) => {
    return request.get<any,searchBarData>(
      `/bar/serch?page=${page}&pagesize=${pagesize}&name=${name}`
    );
  },
  // 修改贴吧信息
  requestEditBar:(data:any)=>{
    return request.patch<any,ApifoxModal>(`/bar/${data._id}`,data)
  },

  // 创建 帖子
  requestCreateDynamic: (param: typeCreateDynamicParam) => {
    return request.post("proposition", param);
  },

  //关注贴吧
  requestFocusOnBar: (barId: number) => {
    return request.get<any, any>(`/bar/subBar/${barId}`);
  },
};
